<template>
    <view class="wechat">
        <view class="wechat-line wechat-border">
            <view class="wechat-label">提现至</view>
            <view class="wechat-type">微信零钱包</view>
        </view>
        <view class="wechat-money">
            <view class="wechat-label wechat-content">提现金额(元)</view>
            <input
                v-model="amount"
                type="digit"
                @input="handleInput"
                placeholder="请输入提现金额"
                :placeholder-style="'color: #cccc;' + 'font-size:36rpx'" />
        </view>
        <view class="wechat-box" v-if="isTipError">
            <view class="wechat-alert">{{ isTipError }}</view>
        </view>
        <view class="wechat-tips">单笔不得超过￥200</view>
        <view class="wechat-btn">
            <view class="wechat-click" v-if="isMoneyAllow" @click="cash">申请提现</view>
            <view class="wechat-diabled" v-else @click="cash">申请提现</view>
        </view>
    </view>
</template>
<script setup>
import { ref, computed } from 'vue';
import request from '@/request';
import { debounce } from 'lodash';

import router from '@/router';
let amount = ref();

const cash = debounce(async () => {
    uni.showLoading({
        title: '提现中...',
        mask: true
    });
    request('withdrawToWechat', { withdrawMethod: 1, amount: amount.value * 100 }).then(res => {
        router.redirectTo({
            url: '/pages-user/balanceCash/cash/cashStatus'
        });
    });
}, 500);
let isTipError = ref('');
const isMoneyAllow = computed(() => {
    // 不能为空或者0
    if (!amount.value || /^0(\.?0+)?$/.test(amount.value)) {
        return false;
    }
    // 必须是正确的数字
    if (!/^\d+(\.\d+)?$/.test(amount.value)) {
        return false;
    }
    // 必须是两位小数
    if (!/^(\d+)(.\d{0,2})?$/.test(amount.value)) {
        return false;
    }
    return true;
});
const handleInput = debounce(async () => {
    // 提现金额：纯数字，＞0，≤账户余额，最多2位小数；
    // 用户输入0，则按钮淡色，不可点击
    // 用户输入＞0的数字后，就要立即调接口，是否超过最新的可用余额
    if (!amount.value || /^0(\.?0+)?$/.test(amount.value)) {
        isTipError.value = /^0(\.?0+)?$/.test(amount.value) ? '*请输入大于0的数字' : '';
        return;
    }
    if (!/^\d+(\.\d+)?$/.test(amount.value)) {
        isTipError.value = '*请输入正确的数字';
        return;
    }
    if (!/^(\d+)(.\d{0,2})?$/.test(amount.value)) {
        isTipError.value = '*提现金额最多为两位小数';
        return;
    }
    isTipError.value = '';
}, 500);
</script>
<style lang="scss" scoped>
.wechat {
    .wechat-tips {
        padding: 24rpx 29rpx;
        font-size: 24rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #999999;
    }
    .wechat-alert {
        color: #fe292b;
        font-family: PingFangSC-Regular, PingFang SC;
        font-size: 25rpx;
        font-weight: 400;
    }
    .wechat-box {
        padding-top: 20rpx;
        padding-left: 29rpx;
    }
    .wechat-diabled {
        width: 692rpx;
        height: 80rpx;
        border-radius: 40rpx;
        font-size: 29rpx;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #ffffff;
        line-height: 80rpx;
        text-align: center;
        background: linear-gradient(270deg, #ff5356 0%, #fe292b 100%);
        opacity: 0.4;
    }
    .wechat-btn {
        margin-top: 243rpx;
        display: flex;
        justify-content: center;
    }
    .wechat-border {
        border-bottom: 1px solid #f5f5f5;
    }
    .wechat-click {
        width: 692rpx;
        height: 80rpx;
        background: #fe292b;
        border-radius: 40rpx;
        font-size: 29rpx;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #ffffff;
        line-height: 80rpx;
        text-align: center;
    }
    .wechat-money {
        background: #fff;
        padding: 36rpx 29rpx;
        .wechat-content {
            margin-bottom: 43rpx;
        }
    }
    .wechat-line {
        background: #fff;
        padding: 36rpx 29rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .wechat-label {
            font-size: 29rpx;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #666666;
        }

        .wechat-type {
            font-size: 29rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #333333;
        }
    }
}
</style>
